/**
 * @(#)AddTransactionPanel.java
 *
 *
 * @author 
 * @version 1.00 2010/2/20
 */
package drops.gui;

import javax.swing.*;

import drops.control.DentalTransactionControl;
import drops.dao.DentalTransactionPeer;
import drops.dao.Patient;
import drops.dao.PatientPeer;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.List;

public class AddTransactionPanel extends JPanel implements  ActionListener, PropertyChangeListener{
	JLabel dateLabel;
	JLabel patientNumberLabel;
	JLabel descriptionLabel;
	JLabel timeLabel;
	JLabel debitLabel;
	JLabel creditDateLabel;
	JLabel creditAmountLabel;
	JLabel balanceLabel;

	JComboBox patientChooserDropDown;
	ComboBoxModel patientModel;
	
	JFormattedTextField dateField;
	JFormattedTextField patientNumberField;
	JTextField descriptionField;
	JFormattedTextField timeField;
	JFormattedTextField debitField;
	JFormattedTextField creditDateField;
	JFormattedTextField creditAmountField;
	JFormattedTextField balanceField;

	JButton submitButton;
	JButton clearButton;
	
	MainFrame mainFrame;
	
    public AddTransactionPanel (MainFrame mainFrame) {
    	this.mainFrame = mainFrame;
    	
    	dateLabel = new JLabel("Date:");
		patientNumberLabel = new JLabel("Patient Number:");
		descriptionLabel = new JLabel("Description:");
		timeLabel = new JLabel("Time:");
		debitLabel = new JLabel("Debit:");
	 	creditDateLabel = new JLabel("Credit Date:");
	 	creditAmountLabel = new JLabel("Credit Amount:");
	 	balanceLabel = new JLabel("Balance:");
    	
	 	
	 	DateFormat df = DateFormat.getDateInstance();
    	DateFormat tf = DateFormat.getTimeInstance();
    	NumberFormat cf = NumberFormat.getNumberInstance();
	 	dateField = new JFormattedTextField(df);
    	dateField.setText(df.format(new Date()));
    	
    	List <Patient> pList = PatientPeer.retrievePatients();
		String[] patientNumbers = new String[pList.size()];
		for (int i=0, size=pList.size();i<size;i++) {
			Patient temp = pList.get(i);
			patientNumbers[i] = temp.getPatientNumber() + "";
		}
		patientModel = new DefaultComboBoxModel(patientNumbers);
		patientChooserDropDown = new JComboBox(patientModel);
    	
    	descriptionField = new JTextField(50);
    	timeField = new JFormattedTextField(tf);
    	timeField.setValue(new Date());
    	debitField = new JFormattedTextField(cf);
    	debitField.setValue(0.0);
    	creditDateField = new JFormattedTextField(df);
    	creditDateField.setValue(new Date());
    	creditAmountField = new JFormattedTextField(cf);
    	creditAmountField.setValue(0.0);
    	balanceField = new JFormattedTextField(cf);
    	balanceField.setValue(0.0);
    	submitButton = new JButton("Submit");
    	submitButton.addActionListener(this);
    	clearButton = new JButton("Clear");
    	
    	setLayout(new GridLayout(9,2));
    	
    	add(dateLabel);
    	add(dateField);
    	add(patientNumberLabel);
    	add(patientChooserDropDown);
    	add(descriptionLabel);
    	add(descriptionField);
    	add(timeLabel);
    	add(timeField);
    	add(debitLabel);
    	add(debitField);
    	add(creditDateLabel);
    	add(creditDateField);
    	add(creditAmountLabel);
    	add(creditAmountField);
    	add(balanceLabel);
    	add(balanceField);
    	add(submitButton);
    }
    
    public void updatePatientList() {
    	List <Patient> pList = PatientPeer.retrievePatients();
		String[] patientNumbers = new String[pList.size()];
		for (int i=0, size=pList.size();i<size;i++) {
			Patient temp = pList.get(i);
			patientNumbers[i] = temp.getPatientNumber() + "";
		}
		patientModel = new DefaultComboBoxModel(patientNumbers);
    }

    public void addTransaction() {
		Date date = (Date)dateField.getValue();
		Date time = (Date)timeField.getValue();
		Calendar c = Calendar.getInstance();
		Calendar c2 = new GregorianCalendar();
		c.setTime(date);
		c2.setTime(time);
		c.set(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c2.get(Calendar.MINUTE));
		c.set(Calendar.SECOND, c2.get(Calendar.SECOND));
		date = c.getTime();
		int patientNumber = Integer.parseInt(patientModel.getSelectedItem().toString());
		String description = descriptionField.getText();
		double debit = Double.parseDouble(debitField.getValue().toString());
		Date creditDate = (Date) creditDateField.getValue();
		double creditAmount = Double.parseDouble(creditAmountField.getValue().toString());
		double balance = Double.parseDouble(balanceField.getValue().toString());
		if (DentalTransactionControl.addDentalTransaction(patientNumber, date, description, debit, creditDate, creditAmount, balance)!=-1) {
			JOptionPane.showMessageDialog(this, "Transaction Successfully added.");
			mainFrame.refresh();
			mainFrame.closeAddTransactionFrame();
		}
		else {
			JOptionPane.showMessageDialog(this, "Add Transaction failed.");
		}
    }
    
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==submitButton) {
			addTransaction();
		}
	}
	
	public void setPatientNumber(int patientNumber) {
		patientModel.setSelectedItem(patientNumber+"");
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}